package com.itheima.springbootwebtlias.controller;

import com.itheima.springbootwebtlias.pojo.*;
import com.itheima.springbootwebtlias.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    //学员列表查询
    @GetMapping
    public Result page(StudentQueryParam queryParam){
        //1.接收请求参数：kv格式
        //定义分页条件实体对象+kv格式参数
        log.info("分页条件查询，请求参数：{}",queryParam);
        //2.调用业务层功能方法，查询员工列表
        PageResult pageResult = studentService.page(queryParam);
        //3.封装返回结果
        return Result.success(pageResult);
    }

    //添加学员
    @PostMapping
    public Result add(@RequestBody Student student){
        log.info("添加员工，请求参数：{}",student);
        studentService.add(student);
        return Result.success();
    }

    //根据id查询学生
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("根据id查询员工，请求参数：{}",id);
        Student student = studentService.selectById(id);
        return Result.success(student);
    }

    //修改学员信息
    @PutMapping
    public Result modifyStudent(@RequestBody Student student){
        log.info("修改学员信息，请求参数:{}",student);
        studentService.modifyStudent(student);
        return Result.success();
    }

    //删除学员
    @DeleteMapping("/{ids}")
    public Result deletes(@PathVariable List<Integer> ids){
        log.info("删除学员，请求参数：{}",ids);
        studentService.deleteBatch(ids);
        return Result.success();
    }

    //学员违纪处理
    @PutMapping("/violation/{id}/{score}")
    public Result violate(@PathVariable Integer id,@PathVariable Short score){
        studentService.violate(id,score);
        return Result.success();
    }
}
